using Spring.Context;
using System;
using System.Collections;
using System.Diagnostics;
using System.Collections.Generic;
namespace AppToolKit.Core.Context.Spring
{
	public class ContextHelper
	{
		private IApplicationContext context_;
		public ContextHelper(IApplicationContext context)
		{
			this.context_ = context;
		}
		public object GetObject(Type type)
		{
			IDictionary<string,object> objectsOfType = this.context_.GetObjectsOfType(type);
			if (objectsOfType.Values.Count != 1)
			{
				throw new ContextAccessException(string.Format("获取对象出错,匹配类型{0}的对象没有定义或多于一个", type.FullName));
			}
			IEnumerator enumerator = objectsOfType.Values.GetEnumerator();
			enumerator.MoveNext();
			return enumerator.Current;
		}
		public bool ContainsObject(Type type)
		{
			Debug.Assert(this.context_ != null);
            IDictionary<string, object> objectsOfType = this.context_.GetObjectsOfType(type);
			bool result;
			if (objectsOfType != null && objectsOfType.Values.Count < 1)
			{
				result = false;
			}
			else
			{
				if (objectsOfType != null && objectsOfType.Values.Count > 1)
				{
					throw new ContextAccessException(string.Format("获取对象出错,匹配类型{0}的对象定义多于一个", type.FullName));
				}
				result = true;
			}
			return result;
		}
		public string GetObjectName(Type type)
		{
			IList<string> objectNamesForType = this.context_.GetObjectNamesForType(type);
			if (objectNamesForType.Count != 1)
			{
				throw new ContextAccessException(string.Format("类型({0}),name没有找到或多于一个", type.FullName));
			}
			return objectNamesForType[0];
		}
		public object GetObject(string name)
		{
			return this.context_.GetObject(name);
		}
		public bool ContainsObjectDefine(string name)
		{
			return this.context_.ContainsObjectDefinition(name);
		}
	}
}
